// SubsqDiagQueryPage.cpp : implementation file
//

#include "stdafx.h"
#include "../Frame/GYWalkAidApp.h"
#include "SubsqDiagQueryPage.h"
#include "../Common/CommonFun.h"
#include "../Common/GYDataSet.h"
#include "PatMan.h"
#include "../Common/LinkInputNode.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSubsqDiagQueryPage dialog
static INPUT_FIELD_INFO s_InputFieldArray[] = 
{
	{NULL,		IBT_NoneType,	FALSE,	FALSE,	NULL,			ftDate,		FALSE,	10,		NULL,	NULL	},
	{NULL,		IBT_NoneType,	FALSE,	FALSE,	NULL,			ftDate,		FALSE,	10,		NULL,	NULL	},
};

CSubsqDiagQueryPage::CSubsqDiagQueryPage(CWnd* pParent /*=NULL*/) : CPageDialog(CSubsqDiagQueryPage::IDD, pParent)
,	m_edtBeginDate(&s_InputFieldArray[0])
,	  m_edtEndDate(&s_InputFieldArray[1])
{
	//{{AFX_DATA_INIT(CSubsqDiagQueryPage)
	m_strTotolSteps = _T("");
	m_strTotalTime = _T("");
	//}}AFX_DATA_INIT
	
	m_bInited = FALSE;
}


void CSubsqDiagQueryPage::DoDataExchange(CDataExchange* pDX)
{
	CPageDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSubsqDiagQueryPage)
	DDX_Control(pDX, IDC_EDIT_START_DATE, m_edtBeginDate);
	DDX_Control(pDX, IDC_EDIT_END_DATE, m_edtEndDate);
	DDX_Control(pDX, IDC_LIST_WA_HIS_DATA, m_listHistory);
	DDX_Control(pDX, IDC_LIST_SUBSQT_DIAG, m_listDiag);
	DDX_Text(pDX, IDC_STATIC_STEP_COUNT, m_strTotolSteps);
	DDX_Text(pDX, IDC_STATIC_USE_TIMESPAN, m_strTotalTime);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSubsqDiagQueryPage, CPageDialog)
	//{{AFX_MSG_MAP(CSubsqDiagQueryPage)
	ON_BN_CLICKED(IDB_BUTTON_NEW_SDIAG, OnButtonNewSdiag)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_SUBSQT_DIAG, OnDblclkListSubsqtDiag)
	ON_BN_CLICKED(IDC_BUTTON_STAT_QUERY, OnButtonStatQuery)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSubsqDiagQueryPage message handlers

BOOL CSubsqDiagQueryPage::OnInitDialog()
{
	CPageDialog::OnInitDialog();

	CLinkInputNode::EnableCheckValid(TRUE, IBT_AllType, this);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CSubsqDiagQueryPage::Prepare()
{
	if(m_bInited)
		return TRUE;
	
	m_listDiag.InsertColumn(0,	GetResString(IDS_QRY_INDEX),				LVCFMT_CENTER,	70);
	m_listDiag.InsertColumn(1,	GetResString(IDS_PAT_FIELD_MRN),			LVCFMT_CENTER,	100);
	m_listDiag.InsertColumn(2,	GetResString(IDS_QRY_TIME),					LVCFMT_CENTER,	170);
	m_listDiag.InsertColumn(3,	GetResString(IDS_QRY_DOCTOR),				LVCFMT_CENTER,	125);
	m_listDiag.InsertColumn(4,	GetResString(IDS_QRY_UPTIME),				LVCFMT_CENTER,	85);
	m_listDiag.InsertColumn(5,	GetResString(IDS_QRY_DOWNTIME),				LVCFMT_CENTER,	85);
	m_listDiag.InsertColumn(6,	GetResString(IDS_SUBSQT_STI_OPEN),			LVCFMT_CENTER,	105);
	m_listDiag.InsertColumn(7,	GetResString(IDS_SUBSQT_STI_CLOSE),			LVCFMT_CENTER,	105);
	m_listDiag.InsertColumn(8,	GetResString(IDS_QRY_STIFREQ),				LVCFMT_CENTER,	60);
	m_listDiag.InsertColumn(9,	GetResString(IDS_QRY_STIWIDTH),				LVCFMT_CENTER,	60);
	m_listDiag.InsertColumn(10,	GetResString(IDS_QRY_STICOUNT),				LVCFMT_CENTER,	70);
	m_listDiag.InsertColumn(11, GetResString(IDS_QRY_WA),					LVCFMT_CENTER,	100);
	m_listDiag.InsertColumn(12, GetResString(IDS_QRY_DRAFT),				LVCFMT_CENTER,	50);

	DWORD dwStyleEx = m_listDiag.GetExtendedStyle();
	dwStyleEx |= LVS_EX_FULLROWSELECT;
	dwStyleEx |= LVS_EX_ONECLICKACTIVATE;
	m_listDiag.SetExtendedStyle(dwStyleEx);


	m_listHistory.InsertColumn(0,	GetResString(IDS_QRY_INDEX),				LVCFMT_CENTER,	50);
	m_listHistory.InsertColumn(1,	GetResString(IDS_PAT_FIELD_MRN),			LVCFMT_CENTER,	100);
	m_listHistory.InsertColumn(2,	GetResString(IDS_QRY_USETIME),				LVCFMT_CENTER,	200);
	m_listHistory.InsertColumn(3,	GetResString(IDS_QRY_USETIMESPAN),			LVCFMT_CENTER,	130);
	m_listHistory.InsertColumn(4,	GetResString(IDS_QRY_STEPCOUNT),			LVCFMT_CENTER,	90);
	
	dwStyleEx = m_listHistory.GetExtendedStyle();
	dwStyleEx |= LVS_EX_FULLROWSELECT;
	dwStyleEx |= LVS_EX_ONECLICKACTIVATE;
	m_listHistory.SetExtendedStyle(dwStyleEx);

	m_bInited = TRUE;
	return TRUE;
}

BOOL CSubsqDiagQueryPage::ClearUp()
{
	return TRUE;
}

BOOL CSubsqDiagQueryPage::ShowTableData(CString strMRN)
{
	Prepare();

	ShowDiagTableInfo(strMRN);

	ShowHistoryTableInfo(strMRN);

	return TRUE;
}

void CSubsqDiagQueryPage::OnButtonNewSdiag() 
{
	CPatMan* pParent = (CPatMan*)GetParent();
	if(pParent == NULL)
		return;

	CSubsqtDiagPage* pSDiagPage =  (CSubsqtDiagPage*)pParent->GetTabPage(TAB_INDEX_SUBSDIAG);
	if(pSDiagPage == NULL)
		return;

	if(pParent->IsTabEnabled(TAB_INDEX_SUBSDIAG) && pSDiagPage->IsControlsEnable())
	{
		pParent->EnableTab(TAB_INDEX_SUBSDIAG, TRUE);
		pParent->SelectTab(TAB_INDEX_SUBSDIAG);
		return;
	}

	CString strMRN = GetApp()->GetCurMRN();
	CString strSQL;
	strSQL.Format(_T("SELECT DiagSN FROM DiagRecord WHERE MedicalRecordNo='%s' AND DiagSN > 0 ORDER BY DiagSN DESC LIMIT 1"), strMRN);
	
	int nNewDiagSN = 0;
	CGYDataSet setDiagRec(strSQL);
	if(setDiagRec.IsEOF())
	{
		nNewDiagSN = 1;
	}
	else
	{
		CString strDiagSN;
		setDiagRec.GetFieldValue(_T("DiagSN"), strDiagSN);

		nNewDiagSN = _ttoi(strDiagSN) + 1;
	}

	pParent->EnableTab(TAB_INDEX_SUBSDIAG, TRUE);
	pSDiagPage->NewSdiag(nNewDiagSN);
	
}

void CSubsqDiagQueryPage::OnDblclkListSubsqtDiag(NMHDR* pNMHDR, LRESULT* pResult) 
{
	CPatMan* pParent = (CPatMan*)GetParent();
	if(pParent == NULL)
		return;

	int nListCurSel = -1;
	POSITION pos = m_listDiag.GetFirstSelectedItemPosition();
	if(pos)
		nListCurSel = m_listDiag.GetNextSelectedItem(pos);

	if(nListCurSel < 0)
		return;

	CSubsqtDiagPage* pSDiagPage =  (CSubsqtDiagPage*)pParent->GetTabPage(TAB_INDEX_SUBSDIAG);
	if(pSDiagPage == NULL)
		return;

	int nDiagSN = nListCurSel;
	CString strMRN = GetApp()->GetCurMRN();

	if(nDiagSN  == 0)
	{
		pParent->SelectTab(TAB_INDEX_1STDIAG);
	}
	else
	{
		pSDiagPage->SetDiagSN(nDiagSN);

		pParent->EnableTab(TAB_INDEX_SUBSDIAG, TRUE);
		pParent->SelectTab(TAB_INDEX_SUBSDIAG);
		
		BOOL bIsDraft = FALSE;
		pSDiagPage->ShowTableDataByDiagSN(strMRN, nDiagSN, &bIsDraft);
		pSDiagPage->EnableInputControls(bIsDraft);

		if(bIsDraft)
			pSDiagPage->SetRealtimeState();
	}

	*pResult = 0;
}

void CSubsqDiagQueryPage::ShowDiagTableInfo(CString strMRN)
{
	m_listDiag.DeleteAllItems();
	
	BOOL bHasDrfat = FALSE;
	
	CString strSQL;
	strSQL.Format(_T("SELECT DiagDate, DiagSN, RealName, IsDraft, UpTime, DownTime, StiOpen, StiClose, PulseFreq, PulseWidth, PulseCount, WADevID FROM DiagRecord a, UserInfo b WHERE a.MedicalRecordNo='%s' AND a.UserName=b.UserName ORDER BY DiagSN"), strMRN);
	
	int nLineCount = 0;
	CGYDataSet setDiagRec;
	for(setDiagRec.OpenTable(strSQL); !setDiagRec.IsEOF(); setDiagRec.MoveNext())
	{
		CString strDiagSN;
		setDiagRec.GetFieldValue(_T("DiagSN"), strDiagSN);

		if(_ttoi(strDiagSN) == 0)
			m_listDiag.InsertItem(nLineCount, GetResString(IDS_QRY_1ST_DIAG));
		else
			m_listDiag.InsertItem(nLineCount, GetResString(IDS_TABTITLE_SUBSEDIAG) + _T(" ") + strDiagSN);
		
		m_listDiag.SetItemText(nLineCount, 1, strMRN);

		CString strValue;
		setDiagRec.GetFieldValue(_T("DiagDate"), strValue);
		m_listDiag.SetItemText(nLineCount, 2, strValue);
		
		setDiagRec.GetFieldValue(_T("RealName"), strValue);
		m_listDiag.SetItemText(nLineCount, 3, strValue);
		
		setDiagRec.GetFieldValue(_T("UpTime"), strValue);
		m_listDiag.SetItemText(nLineCount, 4, strValue);
		
		setDiagRec.GetFieldValue(_T("DownTime"), strValue);
		m_listDiag.SetItemText(nLineCount, 5, strValue);
		
		setDiagRec.GetFieldValue(_T("StiOpen"), strValue);
		m_listDiag.SetItemText(nLineCount, 6, strValue);
		
		setDiagRec.GetFieldValue(_T("StiClose"), strValue);
		m_listDiag.SetItemText(nLineCount, 7, strValue);
		
		setDiagRec.GetFieldValue(_T("PulseFreq"), strValue);
		m_listDiag.SetItemText(nLineCount, 8, strValue);
		
		setDiagRec.GetFieldValue(_T("PulseWidth"), strValue);
		m_listDiag.SetItemText(nLineCount, 9, strValue);
		
		setDiagRec.GetFieldValue(_T("PulseCount"), strValue);
		m_listDiag.SetItemText(nLineCount, 10, strValue);
		
		setDiagRec.GetFieldValue(_T("WADevID"), strValue);
		m_listDiag.SetItemText(nLineCount, 11, strValue);
		
		setDiagRec.GetFieldValue(_T("IsDraft"), strValue);
		m_listDiag.SetItemText(nLineCount, 12, strValue != _T("0") ? GetResString(IDS_QRY_CHECK_SELECT) : _T(""));
		
		if(strValue != _T("0"))
			bHasDrfat = TRUE;
		
		nLineCount++;
	}
	
	if(m_listDiag.GetItemCount() > 0)
	{
		m_listDiag.SetItemState(0, LVIS_SELECTED|LVIS_FOCUSED,  LVIS_SELECTED|LVIS_FOCUSED);
		GetDlgItem(IDB_BUTTON_NEW_SDIAG)->EnableWindow(!bHasDrfat);
	}
	else
	{
		GetDlgItem(IDB_BUTTON_NEW_SDIAG)->EnableWindow(FALSE);
	}
}

void CSubsqDiagQueryPage::ShowHistoryTableInfo(CString strMRN)
{
	ShowHistoryDataByDate(strMRN, GetApp()->GetCurSN(), TRUE);
}

void CSubsqDiagQueryPage::ShowHistoryDataByDate(CString strMRN, DWORD dwSN, BOOL bAllTime)
{
	m_listHistory.DeleteAllItems();

	CString strSQL;
	if(bAllTime)
	{
		strSQL.Format(_T("SELECT UseTime, TimeSpan, StepCount FROM WAHistoryData WHERE MRNCode=%d ORDER BY UseTime"), dwSN);
	}
	else
	{
		CString strBeginDate = m_edtBeginDate.GetControlData();
		CString strEndDate = m_edtEndDate.GetControlData();

		strSQL.Format(_T("SELECT UseTime, TimeSpan, StepCount FROM WAHistoryData WHERE MRNCode=%d AND UseTime>='%s 00:00:00' AND UseTime<='%s 23:59:59' ORDER BY UseTime"), dwSN, strBeginDate, strEndDate);
	}

	CTimeSpan tsTotalTime(0, 0, 0, 0);
	int nTotalStep = 0;

	CTime tMaxTime(1980, 1, 1, 0, 0, 0);
	CTime tMinTime(2037, 12, 31, 23, 59, 59);

	CGYDataSet setHistory(strSQL);
	for(int nRecIndex = 0; !setHistory.IsEOF(); setHistory.MoveNext(), nRecIndex++)
	{
		CString strValue;

		strValue.Format(_T("%d"), nRecIndex);
		m_listHistory.InsertItem(nRecIndex, strValue);

		m_listHistory.SetItemText(nRecIndex, 1, strMRN);

		setHistory.GetFieldValue(_T("UseTime"), strValue);
		strValue.TrimLeft();
		strValue.TrimRight();
		m_listHistory.SetItemText(nRecIndex, 2, strValue);

		CTime tFromTb = CString2CTime(strValue);
		if(tFromTb > tMaxTime)
			tMaxTime = tFromTb;

		if(tFromTb < tMinTime)
			tMinTime = tFromTb;

		setHistory.GetFieldValue(_T("TimeSpan"), strValue);

		int nTotalSeconds	= _ttoi(strValue);
		int nHour			= (nTotalSeconds / 3600);
		int nMinute			= (nTotalSeconds - nHour * 3600) / 60;
		int nSecond			= (nTotalSeconds - nHour * 3600 - nMinute * 60);

		strValue.Format(_T("%02d:%02d:%02d"), nHour, nMinute, nSecond);
		m_listHistory.SetItemText(nRecIndex, 3, strValue);

		setHistory.GetFieldValue(_T("StepCount"), strValue);
		m_listHistory.SetItemText(nRecIndex, 4, strValue);

		CTimeSpan tsTime(0, nHour, nMinute, nSecond);
		tsTotalTime += tsTime;

		nTotalStep += _ttoi(strValue);
	}
	
	if(m_listHistory.GetItemCount() > 0)
	{
		m_strTotalTime.Format(_T("%d %s %d %s %d %s"), tsTotalTime.GetHours(), GetResString(IDS_QRY_HOUR), tsTotalTime.GetMinutes(), GetResString(IDS_QRY_MINUTE), tsTotalTime.GetSeconds(), GetResString(IDS_QRY_SECOND));
		m_strTotolSteps.Format(_T("%d %s"), nTotalStep, GetResString(IDS_QRY_STEP));
	}
	else
	{
		m_strTotalTime.Empty();
		m_strTotolSteps.Empty();
	}

	if(bAllTime)
	{
		if(m_listHistory.GetItemCount() > 0)
		{
			m_edtBeginDate.SetDateTime(tMinTime);
			m_edtEndDate.SetDateTime(tMaxTime);
		}
		else
		{
			m_edtBeginDate.SetToday();
			m_edtEndDate.SetToday();
		}

		UpdateData(FALSE);
	}

	// the following 2 controls are disabled by CLinkInputNode::EnableInputs, enable them here
	GetDlgItem(IDC_EDIT_START_DATE)->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_END_DATE)->EnableWindow(TRUE);
}

void CSubsqDiagQueryPage::OnButtonStatQuery() 
{
	CString strBeginDate = m_edtBeginDate.GetControlData();
	CString strEndDate = m_edtEndDate.GetControlData();

	if(strBeginDate > strEndDate)
	{
		GYMsgBox(IDS_QRY_BEGIN_LATER_END, MB_OK|MB_ICONINFORMATION);
		return;
	}

	CTime tCur = CTime::GetCurrentTime();
	CString strToday = tCur.Format(_T("%Y-%m-%d"));
	
	if(strBeginDate > strToday)
	{
		GYMsgBox(IDS_QRY_BEGINDATE_THAN_TODAY, MB_OK|MB_ICONINFORMATION);
		return;
	}

	if(strEndDate > strToday)
	{
		GYMsgBox(IDS_QRY_ENDDATE_THAN_TODAY, MB_OK|MB_ICONINFORMATION);
		return;
	}

	CWaitCursor wait;
	ShowHistoryDataByDate(GetApp()->GetCurMRN(), GetApp()->GetCurSN(), FALSE);
}

CTime CSubsqDiagQueryPage::CString2CTime(CString strTime)
{
	strTime.TrimLeft();
	strTime.TrimRight();

	COleVariant vtime(strTime);
	vtime.ChangeType(VT_DATE);
	COleDateTime cOleTime=vtime;
	SYSTEMTIME systime;
	VariantTimeToSystemTime(cOleTime, &systime);
	CTime cTimeFromDB(systime);

	return cTimeFromDB;
}